import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread("test.jpg")
assert isinstance(img, np.ndarray)

# 色道改变
# 语法：cv2.cvtColor(input_image, flag)
# 参数：input_image：进行颜色空间转换的图像
#           flag：转换类型
# 常用两种
# cv2.COLOR_BGR2GRAY：BGR->GRAY
# cv2.COLOR_BGR2HSV：BGR->HSV

img3 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_color = cv2.cvtColor(img3, cv2.COLOR_GRAY2BGR)  # 将灰度图像转换为彩色图像
plt.imshow(img_color[:, :, ::-1])
plt.show()

# 读取BGR图像
bgr_image = cv2.imread("test.jpg")

# 将BGR图像转换为灰度图像
gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)

# 将BGR图像转换为HSV图像
hsv_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2HSV)

# 创建窗口并显示图像
cv2.namedWindow("BGR Image", cv2.WINDOW_NORMAL)
cv2.imshow("BGR Image", bgr_image)

cv2.namedWindow("Gray Image", cv2.WINDOW_NORMAL)
cv2.imshow("Gray Image", gray_image)

cv2.namedWindow("HSV Image", cv2.WINDOW_NORMAL)
cv2.imshow("HSV Image", hsv_image)

# 等待按下任意键后关闭窗口
cv2.waitKey(0)
cv2.destroyAllWindows()
